package com.shujia.flink.table

import org.apache.flink.configuration.Configuration
import org.apache.flink.streaming.api.scala.StreamExecutionEnvironment
import org.apache.flink.table.api.EnvironmentSettings
import org.apache.flink.table.api.bridge.scala.StreamTableEnvironment

object Demo6FlinkOnHbase {
  def main(args: Array[String]): Unit = {


    //设置其它flink运行参数
    val configuration = new Configuration()
    configuration.setLong("akka.ask.timeout", 100)

    //创建flink 环境
    val bsEnv: StreamExecutionEnvironment = StreamExecutionEnvironment
      .createLocalEnvironment(1,configuration)

    //设置table 环境的一些参数
    val bsSettings: EnvironmentSettings = EnvironmentSettings.newInstance()
      .useBlinkPlanner() //使用blikc计划器
      .inStreamingMode() //流模式
      .build()

    // 创建flink  table 环境
    val bsTableEnv: StreamTableEnvironment = StreamTableEnvironment.create(bsEnv, bsSettings)


    /**
      * 创建读取文件的表  -- 有界流
      *
      */

    bsTableEnv.executeSql(
      """
        |CREATE TABLE student (
        |  id STRING,
        |  name STRING,
        |  age INT,
        |  gender STRING,
        |  clazz STRING
        |)  WITH (
        |  'connector' = 'filesystem',
        |  'path' = 'data/students.json',
        |  'format' = 'json'
        |)
        |
      """.stripMargin)

    /**
      * 将数据保存到hbasez中
      *
      * 需要x先在hbase中创建表
      */


    bsTableEnv.executeSql(
      """
        |
        |CREATE TABLE student_hbase (
        | id STRING,
        | info ROW<name STRING, age INT , gender STRING, clazz STRING>,
        | PRIMARY KEY (id) NOT ENFORCED
        |) WITH (
        | 'connector' = 'hbase-1.4',
        | 'table-name' = 'student',
        | 'zookeeper.quorum' = 'master:2181,node1:2181,node2:2181'
        |)
        |
      """.stripMargin)

    //将数据写入hbase
    /*    bsTableEnv.executeSql(
          """
            |insert into student_hbase
            |
            |select id, ROW(name,age,gender,clazz) as info from student
            |
          """.stripMargin)*/

    /**
      *
      * 读取hbase中的数据 -- 有界流
      *
      */

    bsTableEnv.executeSql(
      """
        |CREATE TABLE print_table (
        |clazz STRING,
        |c BIGINT
        |)
        |WITH ('connector' = 'print')
        |
      """.stripMargin)

    bsTableEnv.executeSql(
      """
        |insert into print_table
        |select info.clazz as clazz, count(1) as c from
        |student_hbase
        |group by info.clazz
        |
      """.stripMargin)


  }

}
